package com.snail.poker.service.impl;

import java.io.InputStream;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.snail.poker.dao.InningsDao;
import com.snail.poker.model.Dictionary;
import com.snail.poker.model.Innings;
import com.snail.poker.model.Opponents;
import com.snail.poker.model.Rank;
import com.snail.poker.service.DictionaryService;
import com.snail.poker.service.InningService;
import com.snail.poker.uitls.DateUtil;
import com.snail.poker.uitls.excel.ExcelExport;
import com.snail.poker.web.bean.PageBean;

/**
 * 牌局的业务逻辑接口
 * 
 * @author Snail
 * 
 */
public class InningServiceImpl implements InningService {
	private InningsDao inningsDao;
	private DictionaryService dictService;

	public DictionaryService getDictService() {
		return dictService;
	}

	public void setDictService(DictionaryService dictService) {
		this.dictService = dictService;
	}

	public InningsDao getInningsDao() {
		return inningsDao;
	}

	public void setInningsDao(InningsDao inningsDao) {
		this.inningsDao = inningsDao;
	}

	@Override
	public void addInnings(Innings innings, List<Rank> ranks,
			List<Opponents> opponents) throws Exception {
		// TODO Auto-generated method stub
		if (null != ranks) {
			Set<Rank> set = new HashSet<Rank>();
			for (Rank r : ranks) {
				set.add(r);
			}
			innings.setRanks(set);
		}
		if (null != opponents) {
			Set<Opponents> set = new HashSet<Opponents>();
			for (Opponents o : opponents) {
				set.add(o);
			}
			innings.setOpponents(set);
		}
		this.inningsDao.addInnings(innings);
	}

	@Override
	public void delInnings(int inning) throws Exception {
		// TODO Auto-generated method stub
		this.inningsDao.delInnings(inning);
	}

	@Override
	public PageBean findInnings(Innings innings, int insureAmong) {
		// TODO Auto-generated method stub
		Dictionary dict = this.dictService.getDict(insureAmong);
		Double[] among = new Double[2];
		if (null != dict) {
			among[0] = Double.valueOf(dict.getDictValue1());
			if ("-1".equals(dict.getDictValue2())) {
				among[1] = null;
			} else {
				among[1] = Double.valueOf(dict.getDictValue2());
			}
		}
		return this.inningsDao.findInnings(innings, among);
	}

	@Override
	public Innings getInnings(int inning) throws Exception {
		// TODO Auto-generated method stub
		return this.inningsDao.getInnings(inning);
	}

	@Override
	public PageBean queryInnings() {
		// TODO Auto-generated method stub
		return this.inningsDao.queryInnings();
	}

	@Override
	public void modifyInnings(Innings innings) {
		// TODO Auto-generated method stub
		this.inningsDao.modifyInnings(innings);
	}

	@Override
	public InputStream searchInnings(Innings innings, int insureAmong) {
		// TODO Auto-generated method stub
		Dictionary dict = this.dictService.getDict(insureAmong);
		Double[] among = new Double[2];
		if (null != dict) {
			among[0] = Double.valueOf(dict.getDictValue1());
			if ("-1".equals(dict.getDictValue2())) {
				among[1] = null;
			} else {
				among[1] = Double.valueOf(dict.getDictValue2());
			}
		}
		List<Innings> list = this.inningsDao.searchInnings(innings, among);

		return ExcelExport.createExcel(list, Innings.class.getName(), DateUtil
				.getDate("yyyy-MM-dd"));
	}
}